﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Zyg.Utility.Helper;
using System.Text;
using System.Web.Services;

namespace Zyg.SSO.Web
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://sso.zyg.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ProcessLogin : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            Zyg.SSO.Core.Business.Account account = context.Session["User"] as Zyg.SSO.Core.Business.Account;

            if (account == null)
            {
                context.Response.Write("{success: false, html: '登录超时,请重新登录'}");
                return;
            }

            string id = context.Request.QueryString["appId"];

            bool paresed;

            Guid appId = id.ToGuid(out paresed);

            context.Response.ContentType = "application/json";

            if (!paresed)
            {
                context.Response.Write("{success: false, html: '非法的应用标识'}");
                return;
            }

            try
            {
                Zyg.SSO.Core.Business.App app = Zyg.SSO.Core.Business.App.Load(appId);

                Zyg.SSO.Core.Business.AccountApp accountApp = Zyg.SSO.Core.Business.AccountApp.Load(account.Id, app.Id);

                StringBuilder sb = new StringBuilder();

                #region original

                /*sb.Append("{success: true, html: \"");
            sb.Append("<table id='userNames' cellpadding='0' cellspacing='0' border='0' class='infoTable' width='98%' align='center'><tr><th>编号</th><th>用户名</th><th></th></tr>");
            if (accountApp.Users.Count > 0)
            {
                for (int i = 0; i < accountApp.Users.Count; i++)
                {
                    sb.Append(string.Format("<tr><td>{0}</td><td>{1}</td><td><a href='PassLogin.aspx?APID={2}&id={3}' target='_blank'>登录</a></td></tr>", i + 1, accountApp.Users[i].Name, id, accountApp.Users[i].Id));
                }
            }
            else
            {
                sb.Append("<tr>暂未设定用户,请先做用户映射</tr>");
            }
            sb.Append("</table>");
            sb.Append("\"}");*/

                #endregion

                #region new

                sb.Append("{success: true, users: [");
                if (accountApp.Users.Count > 0)
                {
                    for (int i = 0; i < accountApp.Users.Count; i++)
                    {
                        sb.Append("{id: '" + accountApp.Users[i].Id.ToString() + "', name: \"" + context.Server.UrlEncode(accountApp.Users[i].Name) +"\"},");
                    }

                    sb.Remove(sb.Length - 1, 1);
                }
                sb.Append("]}");

                #endregion

                context.Response.Write(sb.ToString());
            }
            catch
            {
                context.Response.Write("{success: false, html: '系统出现异常,请稍后再试'}");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
